package cn.virens.server.netty_tcp.packet;

import cn.virens.common.utils.number.ByteUtil;
import cn.virens.server.common.WritePacket;
import cn.virens.server.netty_tcp.TCPServerChannel;

public class TcpWritePacket extends Packet implements WritePacket {

	public TcpWritePacket(TCPServerChannel channel) {
		super(channel);
	}

	@Override
	public void setSeq(int seq) {
		this.seq = seq % 255;
	}

	@Override
	public void setLength(int len) {
		this.length = len;
	}

	@Override
	public void setCommand(int cmd) {
		this.command = cmd;
	}

	@Override
	public void setData(byte[] data) {
		this.data = data;
	}

	@Override
	public void setData(String hex) {
		this.data = ByteUtil.toByte(hex);
	}

	@Override
	public byte[] seqByte() {
		return ByteUtil.toByte(seq, 1);
	}

	@Override
	public byte[] lenByte() {
		return ByteUtil.toByte(length, 2);
	}

	@Override
	public byte[] cmdByte() {
		return ByteUtil.toByte(command, 1);
	}

	@Override
	public byte[] dataByte() {
		return data;
	}

	@Override
	public String toString() {
		return "WritePacket [cmd=" + command + ", data=" + ByteUtil.toHex(data) + "]";
	}
}
